﻿using Microsoft.EntityFrameworkCore;
using QModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QServices
{
	public class SqliteDBHelper
	{

		public static string GetCurrentDBPath()
		{
			var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,  "LocalDB", DateTime.Now.ToString("yyyyMMdd"), "cardb.db");
			
			var datasource = $"data source={path}";
			return datasource;
		}
		public static void InitDB(string path)
		{
			if (!File.Exists(path))
			{
				var dir= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LocalDB", DateTime.Now.ToString("yyyyMMdd"));
				if (!Directory.Exists(dir))
				{
					 Directory.CreateDirectory(dir);
					
				}
				
			}
		}
		public static string InitTableSql = """
		PRAGMA foreign_keys = OFF;
		DROP TABLE IF EXISTS "main"."AGVCarStatusModel";
		CREATE TABLE "AGVCarStatusModel" (
		"Id"  TEXT NOT NULL,
		"CarNumber"  TEXT,
		"IsOpen"  INTEGER,
		"ChongDian"  INTEGER,
		"TongXinOK"  INTEGER,
		"GuZhangQuanBu"  INTEGER,
		"WeiLiaoZhong"  INTEGER,
		"WeiLiaoWanCheng"  INTEGER,
		"GuZhangLeft"  INTEGER,
		"GuZhangRight"  INTEGER,
		"GuZhangFeedLeft"  INTEGER,
		"GuZhangFeedRight"  INTEGER,
		"EmergencyStop"  INTEGER,
		"BatteryAlarm"  INTEGER,
		"ChuGuiLeft"  INTEGER,
		"ChuGuiRight"  INTEGER,
		"CornersTimedOut"  INTEGER,
		"FeedCups"  INTEGER,
		"CurrentBattery"  INTEGER,
		"ServoStatus_L1"  INTEGER,
		"ServoStatus_L2"  INTEGER,
		"ServoStatus_L3"  INTEGER,
		"ServoStatus_L4"  INTEGER,
		"ServoStatus_R1"  INTEGER,
		"ServoStatus_R2"  INTEGER,
		"ServoStatus_R3"  INTEGER,
		"ServoStatus_R4"  INTEGER,
		"ServoUseStatus"  INTEGER,
		"BarCode"  TEXT,
		"FeedTotal"  TEXT,
		"IsOK"  INTEGER,
		"IsCopy" INTEGER,
		"CreateTime"  TEXT,
		"FeedNumber"  INTEGER,
		"BasePointLR"  TEXT,
		PRIMARY KEY ("Id" ASC)
		);


		""";

		public static void InitDB_Table()
		{
			if (File.Exists(GetCurrentDBPath())) return;
			InitDB(GetCurrentDBPath());
			var db = new SqliteDbContext();
			db.Database.EnsureCreated();
			db.Database.ExecuteSqlRaw(InitTableSql);
			db.SaveChanges();

		}

	}
}
